package com.hf.date

import org.joda.time.DateTime
import org.joda.time.DateTimeConstants
import org.joda.time.LocalDate

import com.hf.constant.OrderLunchConst
import com.hf.order.OrderPeriodType

class DateUtils {

	static Date getToday() {
		return jodaToDate(getTodayJoda())
	}

	static Date getFetchEmailClocToday() {
		DateTime dateTime = getTodayJoda().toDateTimeAtStartOfDay().plusHours(OrderLunchConst.FETCH_EMAIL_CLOCK)
		return dateTime.toDate()
	}
	
	static LocalDate getJodaLastWorkDayInMonth(LocalDate day = null) {
		LocalDate tempDay = day?: getTodayJoda()
		LocalDate retDay = tempDay.dayOfMonth().withMaximumValue()
		if (retDay.dayOfWeek > 5) {
			retDay = retDay.minusDays(retDay.dayOfWeek - 5)
		}
		return retDay
	}

	static Date getFirstWorkDayInMonth() {
		LocalDate day = getTodayJoda().dayOfMonth().withMinimumValue()

		if (day.dayOfWeek > 5) {
			day = day.plusDays(8 - day.dayOfWeek)
		}
		return jodaToDate(day)
	}
	
	static LocalDate getDayInNextMonth() {
		return getTodayJoda().plusMonths(1)
	}
	
	static int getMaxDayOfPeriod() {
		
	}

	static Date jodaToDate(LocalDate date) {
		return date.toDateTimeAtStartOfDay().toDate()
	}

	static List<LocalDate> getJodaPeriodStartEndDay(int intPeriod = 0 ) {
		List<LocalDate> jodaPeriodStartEndDay = []

		LocalDate todayJoda = getTodayJoda()
		LocalDate dayJoda
		if (intPeriod == 0) {
			dayJoda = todayJoda
		} else if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.week) {
			dayJoda = todayJoda.withMonthOfYear(intPeriod)
		}  else if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.month) {
			dayJoda = todayJoda.withMonthOfYear(intPeriod)
		}  else {
			throw new IllegalArgumentException("")
		}
		if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.week) {
			jodaPeriodStartEndDay[0] = dayJoda.withDayOfWeek(DateTimeConstants.MONDAY)
			jodaPeriodStartEndDay[1] = dayJoda.withDayOfWeek(DateTimeConstants.SUNDAY)
		} else if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.month){
			jodaPeriodStartEndDay[0] = dayJoda.dayOfMonth().withMinimumValue()
			jodaPeriodStartEndDay[1] = dayJoda.dayOfMonth().withMaximumValue()
		} 
		return jodaPeriodStartEndDay
	}

	static List<Date> getPeriodStartEndDay(int intPeriod = 0) {
		List<LocalDate> jodaPeriodStartEndDay = getJodaPeriodStartEndDay(intPeriod)

		Date periodStartDay = jodaToDate(jodaPeriodStartEndDay[0])
		Date periodEndDay = jodaToDate(jodaPeriodStartEndDay[1])

		List<Date> dates =  [periodStartDay, periodEndDay]
		return dates
	}

	static String getPeriodStartEndDayString() {
		List<LocalDate> jodaPeriodStartEndDay = getJodaPeriodStartEndDay()
		LocalDate periodStartDay = jodaPeriodStartEndDay[0]
		LocalDate periodEndDay = jodaPeriodStartEndDay[1]

		return "$periodStartDay - $periodEndDay"
	}
	
	static int getintperiodOfYear() {
		LocalDate todayJoda = getTodayJoda()
		if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.week) {
			return todayJoda.weekOfWeekyear
		} else if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.month){
			return todayJoda.monthOfYear
		} else {
			throw new IllegalArgumentException("")
		}
	}
	
	static int getDayOfPeriod() {
		LocalDate todayJoda = getTodayJoda()
		if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.week) {
			return todayJoda.dayOfWeek
		} else if (OrderLunchConst.curOrderPeriodType == OrderPeriodType.month){
			return todayJoda.dayOfMonth
		} else {
			throw new IllegalArgumentException("")
		}
	}

	static String getTodayInfo() {
		LocalDate todayJoda = todayJoda
		return "$todayJoda(星期$todayJoda.dayOfWeek)"
	}

	static String getDayInfo(Date date) {
		LocalDate dayJoda = new LocalDate(date)
		return "$dayJoda(星期$dayJoda.dayOfWeek)"
	}

	static LocalDate getTodayJoda() {
		return new LocalDate()
	}
}
